import localforage from 'localforage'

const transformToStyle = (transform, translate = true) => {
  let style = ''
  Object.keys(transform).forEach(key => {
    switch (key) {
      case 'rotate':
        style += `rotateZ(${transform[key]}deg) `
        break
      case 'scale':
        style += `scale(${transform[key]}) `
        break
      case 'translate':
        if (translate) {
          style += `translate(${transform[key][0]}px,${transform[key][1]}px) `
        }
        break
      case 'skew':
        style += `skew(${transform[key][0]}deg,${transform[key][1]}deg) `
        break
    }
  })
  return style
}
const hexTorgba = hex =>{
        var sColor = hex.toLowerCase();
        var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
          if(sColor && reg.test(sColor)){
              if(sColor.length === 4){
                  var sColorNew = "#";
                      for(var i=1; i<4; i+=1){
                          sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
                      }
                      sColor = sColorNew;
              }
              var sColorChange = [];
              for(var i=1; i<7; i+=2){
                  sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));
              }
              return `${sColorChange}`
          }else{
              return sColor;
      }
  }
const rgba2color = rgba => {
  return `rgba(${rgba.r},${rgba.g},${rgba.b},${rgba.a})`
}

const setItem = async (key, value)  => {
  return await localforage.setItem(key, value)
}

const getItem = async (key) => {
  return await localforage.getItem(key)
}

const arrayHas = (arr, item) => {
  for (let el of arr) {
    if (el === item) return true
  }
  return false
}

const stopScroll = (el) => {
  el.addEventListener("touchmove", function (e) {
    if (
      e.target.nodeName === 'INPUT' ||
      e.target.nodeName === 'TEXTAREA'
    ) return
    e.preventDefault()
  })
}

const numberFlost = (num) => {
  let value = Math.round(parseFloat(num) * 100) / 100;
  var xsd = value.toString().split(".");
  if (xsd.length == 1) {
    value = value.toString() + ".00";
    return value;
  }
  if (xsd.length > 1) {
    if (xsd[1].length < 2) {
      value = value.toString() + "0";
    }
    return value;
  }
}
const hasClass =(ele,className) =>{
  if(!ele || !className){
      return false
  }
  let classList =ele.className;
  classList =classList.split(" ");
  for(let i=0;i<classList.length;i++){
      if(classList[i] ==className){
          return true
      }
  }
      return false
}
const addClass =(ele,className) =>{

    if(!ele|| !className){
        return
    }
    let classList =ele.className;
    if(hasClass(ele,className)){
        return false
    }else {
      if(classList.length){
        for(let i=0;i<classList.length;i++){
          var str ="";
          str +=classList;
          str +=" ";
          str +=className;
      }
       ele.className =str
      }else{
        ele.className =className
      }
    }
}
const removeClass =(ele,className) =>{
    if(!ele||!className){
        return false
    }
    let oldClassName =ele.className;
    oldClassName =oldClassName.split(" ");
    if(!oldClassName){
      return false
    }
    for(let i=0;i<oldClassName.length;i++){
        if(oldClassName[i] ==className){
          delete oldClassName[i];
        }
    };
    ele.className =oldClassName.join(" ");
}
export {
  transformToStyle,
  rgba2color,
  setItem,
  getItem,
  arrayHas,
  stopScroll,
  numberFlost,
  hexTorgba,
  hasClass,
  addClass,
  removeClass
}
